package com.ajview.admin.controller.system;

import com.ajview.common.core.constant.Constants;
import com.ajview.common.core.domain.dto.LoginDto;
import com.ajview.common.core.domain.enums.ResultCodeEnum;
import com.ajview.common.core.domain.vo.ResultVo;
import com.ajview.common.utils.StringUtils;
import com.ajview.core.web.service.SysLoginService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author ZhangJunJie
 * @Date 2022-09-29
 * <p>
 * 概要：登录接口
 */
@RestController("/sysLogin")
public class SysLoginController {


    @Resource
    private SysLoginService sysLoginService;

    /**
     * 认证登录请求
     *
     * @param loginDto
     * @return
     */
    @PostMapping("login")
    public JSONObject login(@RequestBody @Validated LoginDto loginDto) {
        String token = sysLoginService.loginCore(loginDto.getUsername(), loginDto.getPassword(),
                loginDto.getCode(), loginDto.getUuid());
        if (StringUtils.isEmpty(token)) {
            return ResultVo.error();
        }
        String webToken = Constants.TOKEN_PREFIX + token;
        JSONObject data = new JSONObject();
        data.put(Constants.TOKEN, webToken);
        return ResultVo.result(ResultCodeEnum.LOGIN_SUCCESS, data);
    }
}
